class EventEmitter {
  constructor() {
    this.events = {}
  }
  on(type, cb) {
    if (this.events[type]) {
      this.events[type].push(cb)
    } else {
      this.events[type] = [cb]
    }
  }
  emit(type, ...args) {
    if (!this.events[type]) return
    for (let i = 0; i < this.events[type].length; i++) {
      this.events[type][i].apply(this, args)
    }
  }
  off(type, fn) {
    if (this.events[type]) {
      this.events[type] = this.events[type].filter(e => e !== fn)
    }
  }
  once(type, fn) {
    function _tmpFn() {
      fn()
      this.off(type, _tmpFn)
    }
    this.on(type, _tmpFn)
  }
}

const event = new EventEmitter();

const handle = (...rest) => {
  console.log(rest);
};

event.on("click", handle);

event.emit("click", 1, 2, 3, 4);

event.off("click", handle);

event.emit("click", 1, 2);

event.once("dbClick", () => {
  console.log(123456);
});
event.emit("dbClick");
event.emit("dbClick");
